package co.fitstart.admin.web.controller.content;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import co.fitstart.admin.entity.content.NewsSearch;
import co.fitstart.admin.service.NewsService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.entity.content.News;

import com.demo2do.core.persistence.support.Order;
import com.demo2do.core.support.Page;
import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.core.web.utils.WebUtils;

/**
 * 
 * @author lidan
 *
 */
@Controller
@RequestMapping("/news")
@MenuSetting("menu-content")
public class NewsController {
    
    private static final Logger logger = LoggerFactory.getLogger(NewsController.class);

       @Autowired
        private NewsService newsService;
       
    /**
     * List all news by page
     * 
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-news-management")
    public ModelAndView list(Page page){
        
        ModelAndView modelAndView = new ModelAndView("content/news/news-list");
        
        List<News> news = newsService.listByPage(News.class, 
                                                 new Order("serial", "DESC").add("createTime", "DESC"), 
                                                 page);
        
        modelAndView.addObject("news", news);
        modelAndView.addObject("page", page);
        
        return modelAndView;
    }
    
    /**
     * Display search panel
     * 
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public String search(){
        return "content/news/news-search";
    }
    
    /**
     * Do search news
     * 
     * @param newsSearch
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method=RequestMethod.GET, params="filter")
    @MenuSetting("submenu-news-management")
    public ModelAndView onSearch(NewsSearch newsSearch,
                                 HttpServletRequest request,
                                 Page page){
        
        ModelAndView modelAndView = new ModelAndView("content/news/news-list");    
        
        List<News> news = newsService.search(News.class, newsSearch, page);
        
        modelAndView.addObject("news", news);
        modelAndView.addObject("page", page);
        
        modelAndView.addObject("refer", "search");
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
        
        return modelAndView;
    }
    
    /**
     * Display create panel
     * 
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    @MenuSetting("submenu-news-management")
    public String create(){
        
        return "content/news/news-create";
    }
    
    /**
     * Do create
     * 
     * @param news
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public @ResponseBody String onCreate(News news){
        
        Result result = new Result();
        
        try{
            
            newsService.save(news.initialize());
            
            result.success().message("创建新闻成功");
        
        }catch(Exception e){
            
            logger.error("NewsController#onCreate() error:", e);
            
            result.fail().message("创建新闻失败");
        }
        
        return JsonUtils.toJsonString(result);
    }

    /**
     * Display delete panel
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{newsId}/delete", method = RequestMethod.GET)
    public String delete(@PathVariable("newsId") Long id){
        return "content/news/news-delete";
    }
    
    /**
     * Do delete news
     * 
     * @param newsId
     * @return
     */
    @RequestMapping(value = "/{newsId}", method = RequestMethod.DELETE)
    public @ResponseBody String onDelete(@PathVariable("newsId") Long newsId){
        
        Result result = new Result();
        
        try{
            
            News news = newsService.load(News.class, newsId);
            
            newsService.update(news.disable());
            
            result.success().message("删除新闻成功");
            
        }catch(Exception e){
            
            logger.error("NewsController#onDelete() error:", e); 
            
            result.fail().message("删除新闻失败");
        }
        
        return JsonUtils.toJsonString(result);
    }        
    
    /**
     * Display stick panel
     * 
     * @param newsId
     * @return
     */
    @RequestMapping(value="/{newsId}/stick", method=RequestMethod.GET)
    public ModelAndView stick(@PathVariable("newsId") Long newsId){
        
        ModelAndView modelAndView = new ModelAndView("content/news/news-stick");
        
        modelAndView.addObject("newsId", newsId);
        
        return modelAndView;
    }
    
    /**
     * Do stick news
     * 
     * @param newsId
     * @return
     */
    @RequestMapping(value="/{newsId}/stick", method=RequestMethod.PATCH)
    public @ResponseBody String onStick(@PathVariable("newsId") Long newsId){
        
        Result result = new Result();
        
        try{
            
            News news  = newsService.load(News.class, newsId);
            
            newsService.stick(news, 8);
            
            result.success().message("置顶新闻成功");
            
        }catch(Exception e){
            
            logger.error("NewsController#onHot() error:", e);
            
            result.fail().message("置顶新闻失败");
                    
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * Display unstick news panel
     * 
     * @param newsId
     * @return
     */
    @RequestMapping(value="/{newsId}/unstick", method=RequestMethod.GET)
    public ModelAndView unstick(@PathVariable("newsId") Long newsId){
        
        ModelAndView modelAndView = new ModelAndView("content/news/news-unstick");
        
        modelAndView.addObject("newsId", newsId);
        
        return modelAndView;
        
    }
    
    /**
     * Do unstick news
     * 
     * @param newsId
     * @return
     */
    @RequestMapping(value="/{newsId}/unstick", method=RequestMethod.PATCH)
    public @ResponseBody String onUnstick(@PathVariable("newsId") Long newsId){
        
        Result result = new Result();
        
        try{
            
            News news  = newsService.load(News.class, newsId);
            
            newsService.unstick(news);
                        
            result.success().message("取消置顶成功");
            
        }catch(Exception e){
            
            logger.error("NewsController#onCancelHot() error:", e);
            
            result.fail().message("取消置顶失败");
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * Display news view panel
     * 
     * @param newsId
     * @return
     */
    @RequestMapping(value = "/{newsId}", method = RequestMethod.GET)
    @MenuSetting("submenu-news-management")
    public ModelAndView view(@PathVariable("newsId") Long newsId){
        
        ModelAndView modelAndView = new ModelAndView("content/news/news-view");
        
        News news = newsService.load(News.class, newsId);
        
        modelAndView.addObject("news", news);
        
        return modelAndView;
    }
    
    /**
     * Display edit panel
     * 
     * @param newsId
     * @return
     */
    @RequestMapping(value = "/{newsId}/edit", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("newsId") Long newsId){
        
        ModelAndView modelAndView = new ModelAndView("content/news/news-edit");
        
        News news = newsService.load(News.class, newsId);
        
        modelAndView.addObject("news", news);
        
        return modelAndView;
    }
    
    /**
     * Do edit
     * 
     * @param news
     * @return
     */
    @RequestMapping(value = "/{newsId}", method = RequestMethod.PATCH)
    public @ResponseBody String onEdit(@Preloading("newsId") News news){
        
        Result result = new Result();
        
        try{
            
            newsService.update(news);
            
            result.success().message("修改新闻成功");
            
        }catch(Exception e){
            
            logger.error("NewsController#onEdit() error:", e);
            
            result.fail().message("修改新闻失败");
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * Display edit cover panel
     * 
     * @param newsId
     * @return
     */
    @RequestMapping(value = "/{newsId}/cover", method = RequestMethod.GET)
    public ModelAndView editCover(@PathVariable("newsId") Long newsId){
        
        ModelAndView modelAndView = new ModelAndView("content/news/news-cover-edit");
        
        News news = newsService.load(News.class, newsId);
        
        modelAndView.addObject("news", news);
        
        return modelAndView;
    }
    
    /**
     * Do edit cover
     * 
     * @param newsId
     * @param cover
     * @return
     */
    @RequestMapping(value = "/{newsId}/cover", method = RequestMethod.PATCH)
    public @ResponseBody String onEditCover(@PathVariable("newsId") Long newsId, String cover){
        
        Result result = new Result();
        
        try{
            
            News news = newsService.load(News.class, newsId);
            
            newsService.update(news.editCover(cover));
            
            result.success().message("修改新闻封面成功");
            
        }catch(Exception e){
            
            logger.error("NewsController#onEdit() error:", e);
            
            result.fail().message("修改新闻封面失败");
        }
   
        return JsonUtils.toJsonString(result);
    }
    
    
}
